package util;

import java.io.IOException;
import java.util.Scanner;

import model.XMLProcessor;

public class Communication extends Connector
{

	public Communication(int port) throws IOException
	{
		super(port);
	}

	@Override
	public void receive(String xml)
	{
		@SuppressWarnings("unused")
		String resposta;
		
		System.out.println("\nArquivo xml recebido: ");
		System.out.println(xml);
		System.out.println("");
		
		System.out.println("Processando arquivo recebido...");
		
		XMLProcessor processor = new XMLProcessor(xml);
		resposta = processor.process();
		
		System.out.println("Enviando resposta...");
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("Informe o IP do banco: ");
		String ip = scanner.nextLine();
		System.out.println("Informe a porta de conexao do banco: ");
		int porta = scanner.nextInt();
		
		System.out.println("Resposta enviada...");
		
		sendFile(resposta,ip,porta);
		
		System.out.println("...arquivo processado.");
	}

	@Override
	public void reportException(Exception exception)
	{
		// TODO Auto-generated method stub

	}
	
	public void sendFile(String conteudo, String ip, int porta)
	{
		try 
		{
			this.send(ip, porta, conteudo);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
	}
	
}
